﻿/*-------------------------------------
//
//CopyLeft 2007 开发二组 
//
//文件名：InvoicePrint.cs
//
//文件功能描述：发货单打印.
// 
//创建标识：2007.11.21 曾健铭
//
//修改标识: 2007.11.28 曾健铭
//修改描述: 修正Bug,按规范重新编码..
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
public partial class InvoicePrint : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            string strInvoiceID = string.Empty;
            //string strInvoiceID = "INV-2007103010013";
            try
            {
                strInvoiceID = Request["InvoiceID"].ToString();
                string strPattern = @"INV-\d{13}";//采用正则表达式验证
                if ((strInvoiceID.Length !=17) ||(!Regex.Match(strInvoiceID, strPattern).Success))
                {
                    strInvoiceID = string.Empty;//验证失败
                }
            }
            catch
            {
                strInvoiceID = string.Empty;
            }
            if (strInvoiceID == string.Empty)
            {
                lblInfo.Text = "传入的ID号有错误";
            }
            else
            {
                //绑定数据到Label中
                BindDataToLabel(strInvoiceID);

                string strOrderInfoID = Session["OrderInfoID"].ToString();
                //由订单号获取订单项
                //string strOrderInfoID = "ORD-2007103010003";
                CallWebService(strOrderInfoID);//异步调用
            }
        }

    }

    /// <summary>
    /// 由发货单号获取发货单的信息,将数据绑定到Label
    /// </summary>
    /// <param name="strInvoiceID"></param>
    private void BindDataToLabel(string strInvoiceID)
    {
        localhost.V_Invoice invoice = new localhost.V_Invoice();
        invoice = (new localhost.DocumentService()).GetInvoiceDetail(strInvoiceID);
        if (invoice == null)
        {
            lblInfo.Text = "发货单不存在";
        }
        else
        {
            //收货人信息.
            lblShipName.Text = invoice.ShipToFirstName + invoice.ShipToLastName;//姓名
            lblShipZip.Text = invoice.ShipZip;//邮编
            lblShipAddress.Text = invoice.ShipProvince + "省" + invoice.ShipCity + "市" + invoice.ShipAddress;//地址
            //收货单信息.
            lblInvoiceID.Text = invoice.InvoiceID;//发货单号
            Session["InvoiceID"] = lblInvoiceID.Text;

            lblOrderInfoID.Text = invoice.OrderInfoID;//订单号
            Session["OrderInfoID"] = lblOrderInfoID.Text;

            //lblBillName.Text = invoice.
            int iStoreKeeper = invoice.StorekeeperID;
            lblStoreKeeperName.Text = (new localhost.StaffService()).GetStaffNameByID(iStoreKeeper);//发货人姓名
            lblShipMethod.Text = invoice.ShipMethod;//发货方式
            lblConsignmentTime.Text = invoice.ConsignmentTime.ToString();//发货日期
            lblPhone.Text = invoice.Phone;//收货人电话
            lblTotalPrice.Text = (new localhost.DocumentService()).GetOrderDetail(lblOrderInfoID.Text).TotalPrice.ToString();//发货单总价
        }
        
    }
    /// <summary>
    /// 异步调用,获取订单项
    /// </summary>
    /// <param name="strOrderInfoID"></param>
    private void CallWebService(string strOrderInfoID)
    {
        localhost.DocumentService service = new localhost.DocumentService();
        service.GetOrderItemCompleted += new localhost.GetOrderItemCompletedEventHandler(service_GetOrderItemCompleted);//声明回调函数
        service.GetOrderItemAsync(strOrderInfoID);
    }
    /// <summary>
    /// 回调函数
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void service_GetOrderItemCompleted(object sender, localhost.GetOrderItemCompletedEventArgs e)
    {
        //将数据绑定到GridView.
        gdvOrderItem.DataSource = null;
        if (e.Result.Tables.Count != 0)
        {
            gdvOrderItem.DataSource = e.Result.Tables[0];
        }
        gdvOrderItem.DataBind();
        lblQuantity.Text = gdvOrderItem.Rows.Count.ToString();//获取物品的总件数
    }
}
